package com.ddgame.xiaomi;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Toast;

import com.xiaomi.ad.mediation.MMAdConfig;
import com.xiaomi.ad.mediation.MMAdError;
import com.xiaomi.ad.mediation.splashad.MMAdSplash;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Copyright (C) 2013, Xiaomi Inc. All rights reserved.
 */

public class SplashActivity extends AppCompatActivity {
    private static String AD_TAG_ID = "732f7bbabfdf38be14ad29ae17e0d5df";
    private boolean canJump = false;
    private int timeout = 1000;
    private MMAdSplash mAdSplash;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        timeout = getIntent().getIntExtra("timeout", 1000);
        mAdSplash = new MMAdSplash(this, AD_TAG_ID);
        mAdSplash.onCreate();

        requestAd();
    }

    @Override
    protected void onPause() {
        super.onPause();
        canJump = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (canJump) {
            goHomePage();
        }
        canJump = true;
    }

    private void requestAd() {
        MMAdConfig adConfig = new MMAdConfig();
        adConfig.supportDeeplink = true;
        adConfig.imageHeight = 1920;
        adConfig.imageWidth = 1080;
        //期望广告view的size,单位dp（*必填）
        adConfig.viewWidth = 1080;
        adConfig.viewHeight = 1920;
        adConfig.splashAdTimeOut = timeout;
        adConfig.setSplashActivity(this);
        adConfig.setSplashContainer((ViewGroup) findViewById(R.id.splash_container));
        adConfig.sloganColor = ((ColorDrawable)findViewById(R.id.slogan_view_group).getBackground()).getColor();
        mAdSplash.load(adConfig, new MMAdSplash.SplashAdInteractionListener() {
            @Override
            public void onAdSkip() {
                if (canJump) {
                    goHomePage();
                } else {
                    canJump = true;
                }
            }

            @Override
            public void onError(MMAdError error) {
                showToast("onError");
//                Toast.makeText(com.xiaomi.ad.mediation.demo.ui.splashad.SplashActivity.this, getString(R.string.ad_load_error, error.errorMessage),
//                Toast.LENGTH_SHORT).show();
                goHomePage();
            }

            @Override
            public void onAdShow() {
                showToast("onAdShow");
//                Toast.makeText(com.xiaomi.ad.mediation.demo.ui.splashad.SplashActivity.this, R.string.ad_loaded, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdClicked() {
                showToast("onAdClicked");
//                Toast.makeText(com.xiaomi.ad.mediation.demo.ui.splashad.SplashActivity.this, R.string.ad_click, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdDismissed() {
                if (canJump) {
                    goHomePage();
                } else {
                    canJump = true;
                }

            }
        });
    }

    protected void showToast(String text){
        Toast.makeText(SplashActivity.this, text,
                Toast.LENGTH_LONG).show();
    }

    protected void goHomePage() {
        startActivity(new Intent(this, XMBaseActivity.class));
        finish();
    }

    @Override
    public void onBackPressed() {
        return;
    }
}
